import md5 from 'js-md5'
import qs from 'qs'
import axios from 'axios'
import Cookies from 'js-cookie'

function encryption2(params, url) {
  const promise = new Promise(function (resolve, reject) {
    const timestamp = (Date.parse(new Date()) / 1000).toString()
    // console.log(JSON.parse(Cookies.get('userinfo')));
    if (Cookies.get('userinfo1')) {
      params.adminId = JSON.parse(Cookies.get('userinfo1')).admin_id
      params.token = JSON.parse(Cookies.get('userinfo1')).token
    }
    params.time = timestamp
    const _token = EncryptedCharacter(params)
    params.sign = md5(_token)
    axios.post(url, qs.stringify(params))
      .then((res) => {
        resolve(res)
      })
      .catch((err) => {
        reject(err)
      })
  })
  return promise
}

function EncryptedCharacter(arys) {
  var param = reverse(arys)
  var extra_str = '&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX'
  if (Object.prototype.toString.call(param) !== '[object Object]') {
    return ''
  }
  let queryParam = ''
  for (const key in param) {
    if (param.hasOwnProperty(key)) {
      const value = param[key]
      // if (value) {
      //   queryParam += toQueryPair(key, value.toString());
      // }
      queryParam += toQueryPair(key, value)
    }
  }
  return queryParam.slice(1) + extra_str
}
// 数组转字符串完成加密
function toQueryPair(key, value) {
  if (typeof value === 'undefined') {
    return `&${key}=`
  }
  return `&${key}=${value}`
}
// 对象转数组并倒序
function reverse(arys) {
  var newkey = Object.keys(arys).sort().reverse()
  var newObj = {}
  for (var i = 0; i < newkey.length; i++) {
    newObj[newkey[i]] = arys[newkey[i]]
  }
  return newObj
}
export {
  encryption2
}
const fileSuffixTypeUtil = {
  matchFileSuffixType(fileName) {
    // 后缀获取
    var suffix = ''
    // 获取类型结果
    var result = ''
    try {
      var flieArr = fileName.split('.')
      suffix = flieArr[flieArr.length - 1]
    } catch (err) {
      suffix = ''
    }
    // fileName无后缀返回 false
    if (!suffix) {
      result = false
      return result
    }
    // 图片格式
    var imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif']
    // 进行图片匹配
    result = imglist.some(function (item) {
      return item == suffix
    })
    if (result) {
      result = 'image'
      return result
    }
    // 匹配txt
    var txtlist = ['txt']
    result = txtlist.some(function (item) {
      return item == suffix
    })
    if (result) {
      result = 'txt'
      return result
    }
    // 匹配 excel
    var excelist = ['xls', 'xlsx']
    result = excelist.some(function (item) {
      return item == suffix
    })
    if (result) {
      result = 'excel'
      return result
    }
    // 匹配 word
    var wordlist = ['doc', 'docx']
    result = wordlist.some(function (item) {
      return item == suffix
    })
    if (result) {
      result = 'word'
      return result
    }
    // 匹配 pdf
    var pdflist = ['pdf']
    result = pdflist.some(function (item) {
      return item == suffix
    })
    if (result) {
      result = 'pdf'
      return result
    }
    // 匹配 ppt
    var pptlist = ['ppt']
    result = pptlist.some(function (item) {
      return item == suffix
    })
    if (result) {
      result = 'ppt'
      return result
    }
    // 匹配 视频
    var videolist = ['mp4', 'm2v', 'mkv']
    result = videolist.some(function (item) {
      return item == suffix
    })
    if (result) {
      result = 'video'
      return result
    }
    // 匹配 音频
    var radiolist = ['mp3', 'wav', 'wmv']
    result = radiolist.some(function (item) {
      return item == suffix
    })
    if (result) {
      result = 'radio'
      return result
    }
    // 其他 文件类型
    result = 'other'
    return result
  }
}
export {
  fileSuffixTypeUtil
}